'''
This file contains the image operations that can be performed in AugIm. 

Created on Jan 25, 2010

@author: madsk
'''

from PIL import ImageEnhance, Image

def blur(image, factor = 1.0):
    """
    Blurs the input image to the given factor.
    @type image: PIl.Image
    @param image: The input image.
    @type factor: float
    @param factor: The sharpness factor. 0.0 is the original image, 
                   1.0 yields the maximal blurriness.
    @rtype: PIL.Image
    @return: The blurred image.
    """
    factor = 1.0 - factor
    return ImageEnhance.Sharpness(image).enhance(factor)

def sharpen(image, factor = 1.0):
    """
    Sharpens the input image to the given factor.
    @type image: PIl.Image
    @param image: The input image.
    @type factor: float
    @param factor: The sharpness factor. 0.0 is the original image, 
                   1.0 yields the maximal sharpness.
    @rtype: PIL.Image
    @return: The sharpened image.
    """
    factor = 1.0 + factor
    return ImageEnhance.Sharpness(image).enhance(factor)

def brightness(image, factor = 1.0): 
    """
    A factor of 1.0 gives the original image, 0.0 gives a pure black 
    image.
    """
    return ImageEnhance.Brightness(image).enhance(factor)

def color(image, factor = 1.0): 
    """
    A factor of 1.0 gives the original image, 0.0 gives a black and white 
    image.
    """
    return ImageEnhance.Color(image).enhance(factor)

def contrast(image, factor = 1.0): 
    """
    A factor of 1.0 gives the original image, 0.0 gives a solid grey  
    image.
    """
    return ImageEnhance.Contrast(image).enhance(factor)

def fliphorizontal(image):
    return image.transpose(Image.FLIP_TOP_BOTTOM)

def flipvertical(image):
    return image.transpose(Image.FLIP_LEFT_RIGHT)

def scale(image, size):
    return image.resize(size, Image.ANTIALIAS)
